const Koa = require('koa');
const app = new Koa();
// 错误处理中间件
app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    ctx.status = err.status || 500;
    ctx.body = { error: err.message };
  }
});
// 模拟一个会抛出错误的中间件
app.use(async (ctx) => {
  if (ctx.path === '/error') {
    throw new Error('Something went wrong!');
  }
  ctx.body = 'Hello, Koa!';
});
app.listen(3000, () => {
  console.log('Server is running at http://localhost:3000');
});